#include "game.h"

static int m_load(lua_State *L)
{
	/* get number of arguments, there should be only one */
	int n = lua_gettop(L);
	size_t len;
	const char * filename = lua_tolstring(L, 1, &len);
	Game * g = (Game*)lua_getgame(L);
	g->m_load(filename);
	printf("\"%s\" loaded successfully!\n", filename);
	///* push the average */
	//lua_pushnumber(L, sum / n);
	///* push the sum */
	//lua_pushnumber(L, sum);
	/* return the number of results */
	return 0;
}

static int g_end(lua_State *L)
{
	Game * g = (Game*)lua_getgame(L);
	g->setRunning(false);
	return 0;
}

static int g_charXY(lua_State *L)
{
	Game * g = (Game*)lua_getgame(L);
	// figure out which character to grab
	int index = lua_tointeger(L, 1);
	// grab that character
	Character * c = g->getChar(index);

	lua_pushnumber(L, c->getVector2D().x);
	lua_pushnumber(L, c->getVector2D().y);
	return 2;
}

static int g_mapGet(lua_State *L)
{
	Game * g = (Game*)lua_getgame(L);
	// where on the map?
	int x = lua_tointeger(L, 1);
	int y = lua_tointeger(L, 2);
	// grab that character
	char str[2];
	str[0] = g->mapGet(x, y);
	str[1] = 0;
	lua_pushstring(L, str);
	return 1;
}